import { ref } from 'vue'
import { useLoading } from '@/hooks'

export function useRequest<T>(
  api: () => Promise<T>,
  immediate: boolean = true,
) {
  const { loading, setLoading } = useLoading(true)
  const response = ref<T>()
  // 刷新
  const refresh = () => {
    api().then((res) => {
      response.value = res
    }).finally(() => {
      setLoading(false)
    })
  }
  if (immediate) {
    refresh()
  }
  return { loading, response, refresh }
}
